package ch.zbw.sysventory.server.control;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import ch.zbw.sysventory.server.model.ComputerScanResult;
import ch.zbw.sysventory.server.service.ComputerScanResultService;

@Component
@Controller
public class SearchController {

	@Autowired
	private ComputerScanResultService computerScanResultService;

	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public ModelAndView showSearchForm(Model model) {
		model.addAttribute("searchCombination", "and");
		model.addAttribute("result_list", new ArrayList<ComputerScanResult>());
		return new ModelAndView("search");
	}

	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public ModelAndView search(Model model, @ModelAttribute("searchCombination") String searchCombination, @ModelAttribute("computerName") String computerName,
			@ModelAttribute("ipAddress") String ipAddress, @ModelAttribute("type") String type, @ModelAttribute("cpuType") String cpuType,
			@ModelAttribute("operatingSystem") String operatingSystem) {

		model.addAttribute("searchCombination", searchCombination);
		model.addAttribute("computerName", computerName);
		model.addAttribute("ipAddress", ipAddress);
		model.addAttribute("type", type);
		model.addAttribute("cpuType", cpuType);
		model.addAttribute("operatingSystem", operatingSystem);

		model.addAttribute("result_list", this.computerScanResultService.searchComputerScans(searchCombination, computerName, ipAddress, type, cpuType, operatingSystem));

		return new ModelAndView("search");
	}

}
